package cn.njyd.corejava.thread.myday02;

class SellTicketThread1 implements Runnable {
    private int num = 50;

    private Object lock = new Object();
    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                if (num > 0) {
                    System.out.println(Thread.currentThread().getName() + "卖了一张票，目前还剩[" + (--num) + "]张");
                } else {
                    System.out.println(Thread.currentThread().getName() + "票已经卖完");
                    break;
                }
            }
        }
    }
}


public class SellTicketSysDemo01 {
    public static void main(String[] args) {
        SellTicketThread1 sellTicketThread = new SellTicketThread1();
        Thread t1=new Thread(sellTicketThread,"售票窗口-1");
        Thread t2=new Thread(sellTicketThread,"售票窗口-2");
        Thread t3=new Thread(sellTicketThread,"售票窗口-3");
        t1.start();
        t2.start();
        t3.start();
    }
}
